#!/bin/bash
#
# This script ensures that all `cargo-fuzz` targets are compilable if the `dry` argument is given. Otherwise it does nothing.
# Prerequisite: install `cargo-fuzz` by running `cargo install cargo-fuzz`.

set -e

run_fuzz_target_dry() {
    crate=$1
    target=$2

    pushd rust/$crate

    echo
    echo "Running '$crate::$target' fuzz test (dry)"
    echo
    cargo fuzz run $target -- -runs=0
    echo

    popd # rust/$crate
}

run_fuzz_crate_dry() {
    crate=$1

    pushd rust/$crate
    targets=$(cargo fuzz list)
    popd # rust/$crate

    while read -r target; do
      run_fuzz_target_dry $crate $target
    done < <(printf '%s\n' "$targets")
}

if [[ $1 == "dry" ]]; then
  run_fuzz_crate_dry "tw_encoding"
  run_fuzz_crate_dry "tw_hash"
  run_fuzz_crate_dry "tw_keypair"
fi
